package com.cskaoyan.day18;

import java.io.*;

public class CVFiles {
    public static void main(String[] args) {
        System.out.println("请输入您要复制的文件目录");
        copyFiles("e:/Constructor");
    }

    public static void copyFiles(String path) {
        //查找符合条件的目录或者文件
        String desPath = "";
        File[] files = new File(path).listFiles(file -> (file.isFile() && file.getName().endsWith(".java"))
                || file.isDirectory());
        for (File file : files) {
            String absolutePath = file.getAbsolutePath();
            desPath = absolutePath.substring(0, 3) + "copy\\" + absolutePath.substring(3);
            if (file.isDirectory()) {
                File file1Directory = new File(desPath);
                if (!file1Directory.exists()) {
                    file1Directory.mkdirs();
                }
                copyFiles(absolutePath);
            } else {

                try (BufferedInputStream in = new BufferedInputStream(new FileInputStream(absolutePath));
                     BufferedOutputStream out = new BufferedOutputStream(new FileOutputStream(desPath))) {
                    byte[] bytes = new byte[1024];
                    int count = 0;
                    while ((count = in.read(bytes)) != -1) {
                        out.write(bytes, 0, count);
                    }
                    out.flush();
                } catch (FileNotFoundException e) {
                    e.printStackTrace();
                } catch (IOException e) {
                    e.printStackTrace();
                }

            }
        }

    }
}
